import Vue from 'vue';
import Vuex from 'vuex';

Vue.use(Vuex);

const state = {
    user: '',
    roles: [],
    token: '',
};

const mutations = {
  setUser(state, name) {
    state.user = name
  },
  resetUser(state) {
    state.user = ""
  },
  setRoles(state, roles) {
    state.roles = roles
  },
  resetRoles(state) {
    state.roles = []
  },
  setToken(state, token) {
    state.token = token
  },
  resetToken(state) {
    state.token = ""
  }
};

const actions = {
  setUser ({ commit }, name) {
    commit('setUser', name)
  },
  resetUser ({ commit }) {
    commit('resetUser')
  },
  setRoles ({ commit }, roles) {
    commit('setRoles', roles)
  },
  resetRoles ({ commit }) {
    commit('resetRoles')
  },
  setToken ({ commit }, token) {
    commit('setToken', token)
  },
  resetToken ({ commit }) {
    commit('resetToken')
  },
};

//e.g. store.getters.curr_user
const getters = {
  curr_user: state => {
      return state.user
  },
  curr_roles: state => {
        return state.roles
  },
  curr_token: state => {
        return state.token
  },
};

var store = new Vuex.Store({
  state: state,
  mutations: mutations,
  actions: actions,
  getters: getters
})

export default store
